package frame.util.shiro;

import frame.base.BaseConstant;
import frame.system.to.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private ShiroJdbc shiroJdbc;
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        //System.out.println("执行用户授权");
        // 获取登陆用户消息
        String username = (String) principal.getPrimaryPrincipal();
        // 根据用户查询角色和权限
        Set<String> roles = shiroJdbc.queryRolesByUsername(username);
        Set<String> perms = shiroJdbc.queryPermsByUsername(username);
        // 封装权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(perms);
        return info;
    }

    /**
     * 认证(登陆认证)
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //System.out.println("执行用户登陆认证");
        UsernamePasswordToken loginToken = (UsernamePasswordToken) token;
        String password0 = new String((char[]) token.getCredentials());
        // 密码认证 shiro框架完成
        return new SimpleAuthenticationInfo(loginToken.getUsername(), password0, getName());
    }

    // ----- begin ----- system user login  -----------
    public static void setSystemUserLogin(User user) {
        final Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(BaseConstant.SYSTEM_LOGIN, user);
    }

    public static User getSystemUserLogin() {
        final Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            final Session session = subject.getSession();
            return (User) session.getAttribute(BaseConstant.SYSTEM_LOGIN);
        } else {
            return null;
        }
    }
    public static Integer getSystemUserLoginId() {
        User user = getSystemUserLogin();
        if (user == null) {
            return 0;
        } else {
            return user.getId();
        }
    }
    /**
     * 从shiro session中获取登陆的用户名
     * @return
     */
    public static String getSystemUserLoginUsername() {
        final Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        return username;
    }
    public static String getHost() {
        final Session session = SecurityUtils.getSubject().getSession();
        return session.getHost();
	}
}
